package com.easytodo;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;
import com.androidnetworking.interfaces.StringRequestListener;
import com.easytodo.entity.Item;
import com.easytodo.entity.Project;

import java.util.List;

public class Server {

    public static void insertNewItem(Item item, StringRequestListener listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/add",getService()))
                .addHeaders("uni","20181732")
                .addBodyParameter(item)
                .build()
                .getAsString(listener);
    }
    public static void getItemList(ParsedRequestListener<List<Item>> listener){

        AndroidNetworking.post(String.format("http://%s:8012/item/list",getService()))
                .addHeaders("uni","20181732")
                .build()
                .getAsObjectList(Item.class,listener);
    }
    public static void deleteItem(Integer id,StringRequestListener listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/delete",getService()))
                .addHeaders("uni","20181732")
                .addBodyParameter("id",String.valueOf(id))
                .build()
                .getAsString(listener);
    }
    public static void finishItem(Integer id,StringRequestListener listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/finish",getService()))
                .addHeaders("uni","20181732")
                .addBodyParameter("id",String.valueOf(id))
                .build()
                .getAsString(listener);
    }
    public static void getProject(ParsedRequestListener<List<Project>> listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/project",getService()))
                .addHeaders("uni","20181732")
                .build()
                .getAsObjectList(Project.class,listener);
    }
    public static void getProjectItem(String path,ParsedRequestListener<List<Item>> listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/project/list",getService()))
                .addHeaders("uni","20181732")
                .addBodyParameter("path",path)
                .build()
                .getAsObjectList(Item.class,listener);
    }
    public static void getItem(String id,ParsedRequestListener<Item> listener){
        AndroidNetworking.post(String.format("http://%s:8012/item/me",getService()))
                .addHeaders("uni","20181732")
                .addBodyParameter("id",id)
                .build()
                .getAsObject(Item.class,listener);
    }
    private static String getService(){
       return Config.DEBUG?Config.SERVICE:Config.REMOTE_SERVICE;
    }
}
